<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\db;

class GoodsbackupModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->id=cmf_get_current_admin_id();
    }
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $cause['id']=$id;
        $user=DB::name('user')->where($cause)->find();
        if($user['user_type']==1)
        {
            $where='';
            $session=DB::name('shop')->where($where)->select();
        }else{
            $where['re_guid']=$user['shop_guid'];
            $session=DB::name('shop')->where($where)->select()->toArray();
        }
        return $session;
    }
   /*属性列表*/
   public function index($cause)
   {
       $session=$this->session();
       if($session)
       {
           if($this->id==1)
           {
               $where='';
           }else{
               $where['bc.shop_id']=$session[0]['re_guid'];
           }
           if($cause)
           {
               $where['bc.state_name']=array('like',"%".$cause."%");
           }
           $data=DB::name('goods_backup')->alias('bc')->join('shop sp','sp.re_guid = bc.shop_id')->where($where)->field('bc.*,sp.shop_name')->paginate(10);
           if($data->toArray())
           {
               foreach ($data as $k=>$v)
               {
                   $data->toArray();
                   $date=array();
                   $date=$v;
                   if($v['status']==1){
                       $date['status_name']='启用';
                   }else{
                       $date['status_name']='禁用';
                   }
                   $data->offsetSet($k,$date);
               }

           }
           $array['data']=$data;
           $array['page']=$data->render();
       }else{
           $array['data']="";
           $array['page']="";
       }
       return $array;

   }
   /*
    * 属性类别
    * */
   public function add($data)
   {
      if(isset($data['shop']))
      {
          $array['gd_guid']=guid();
          $array['state_name']=$data['state_name'];
          $array['status']=1;
          $array['shop_id']=$data['shop'];
          $array['c_time']=time();
          $status=Db::name('goods_backup')->insert($array);
      }else{
          $status=0;
      }
       return $status;
   }
   /*修改属性*/
   public function detail($cause,$data)
   {
        $where=$cause;
        $status=DB::name('goods_backup')->where($where)->update($data);
        return $status;
   }
   /*
    * 删除
    * 先判断是否有该类别的商品 然后看 是否有该类别的订单
    * */
   public function del($cause)
   {
       $where['nature']=$cause['gd_guid'];
       $order=DB::name('order_detail')->where($where)->find();
       if($order)
       {
           $status['num']=0;
           $status['con']='类别下有订单，不能删除！';
       }else{
           $goodsclass=DB::name('goods_backup')->where($cause)->delete();
           if($goodsclass)
           {
               $status['num']=1;
               $status['con']='删除成功';
           }else{
               $status['num']=0;
               $status['con']='删除失败！';
           }
       }
       return $status;
   }
   /**
    * 根据id获取列表记录
   */
   public function getGcone($where){
       $status= DB::name('goods_backup')->where($where)->find();
       return $status;
   }

}